<?php
/* SVN FILE: $Id: item_basicsteal.php 247 2009-10-22 15:31:39Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Basic Steal Money item
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 247 $
* @lastrevision $Date: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/items/steal/item_basicsteal.php $
*/

class item_basicsteal extends templateItem
{
	/**
	 * The default name of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultName = "Steal Money";
	
	/**
	 * The default description of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultDescription = "Use this item to steal money from people. Fail and you lose twice the amount you tried.";
	
	// Inherited Properties
	/**
	 * Item setting to allow multiple instances within ibMarket 
	 *
	 * @access	protected
	 * @var		boolean
	 */
	protected $allowMultiple = true;
	
	/**
	 * Constructor to setup the item
	 *
	 * @access	public
	 * @param	ipsRegistry	$registry	IPB registry
	 * @param	array		&$item		The item information
	 */
	public function __construct(ipsRegistry $registry, array &$item)
	{
		parent::__construct($registry, $item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->itemSettings = unserialize($item['item_config']);
			
			$this->item['item_name'] = str_replace(array('{low}','{high}'),array($this->itemSettings['amount_min'],$this->itemSettings['amount_max']),$this->item['item_name']);
		}
		else
		{
			$this->itemSettings['protected'] = array($this->settings['admin_group']);
			$this->itemSettings['amount_min'] = 0;
			$this->itemSettings['amount_max'] = 10000;
			$this->itemSettings['percent_min'] = 100;
			$this->itemSettings['percent_max'] = 50;
		}
		
		$this->itemDescription = str_replace(array('{low}','{high}'),array($this->itemSettings['amount_min'],$this->itemSettings['amount_max']),$this->item['item_description']);
	}
	
	/**
	 * Additional settings when adding/editing the item
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminForm()
	{
		$form['protected'] = ipsRegistry::getClass('output')->generateGroupDropdown('item[protected][]', $this->itemSettings['protected'], true);
		$form['amount_min'] = marketFormatter::currencyFormat(ipsRegistry::getClass('output')->formSimpleInput('item[amount_min]', $this->itemSettings['amount_min'], 10));
		$form['amount_max'] = marketFormatter::currencyFormat(ipsRegistry::getClass('output')->formSimpleInput('item[amount_max]', $this->itemSettings['amount_max'], 10));
		$form['percent_min'] = ipsRegistry::getClass('output')->formSimpleInput('item[percent_min]', $this->itemSettings['percent_min']);
		$form['percent_max'] = ipsRegistry::getClass('output')->formSimpleInput('item[percent_max]', $this->itemSettings['percent_max']);
		return <<<EOF
				<li>
					<label>{$this->lang->words['tags_for_description']}</label>
					<div style='display: inline-block'>
						{low} = {$this->lang->words['desc_min_steal']}
						<br />
						{high} = {$this->lang->words['desc_max_steal']}
						<br />
						{$this->lang->words['tags_settable']}
					</div>
				</li>
				<li>
					<label>{$this->lang->words['groups_protected']}</label>
					{$form['protected']}
				</li>
				<li>
					<label>{$this->lang->words['steal_min_amount']}</label>
					{$form['amount_min']}
				</li>
				<li>
					<label>{$this->lang->words['steal_min_percent']}</label>
					{$form['percent_min']}%
				</li>
				<li>
					<label>{$this->lang->words['steal_max_amount']}</label>
					{$form['amount_max']}
				</li>
				<li>
					<label>{$this->lang->words['steal_max_percent']}</label>
					{$form['percent_max']}%
				</li>

EOF;
	}
	
	/**
	 * Saving to adding/editing the item
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	array	[$input]		Relavent input relating to the item
	 * @return	void
	 */
	public function adminSave(array &$item_info, array $input)
	{
		$setting = array();
		$setting['protected'] = is_array($input['protected']) ? $input['protected'] : array();
		$setting['amount_min'] = numbers::parseInt($input['amount_min']);
		$setting['amount_max'] = numbers::parseInt($input['amount_max']);
		$setting['percent_min'] = numbers::parseFloat($input['percent_min']);
		$setting['percent_max'] = numbers::parseFloat($input['percent_max']);
		
		$item_info['item_config'] = serialize($setting);
	}
	
	/**
	 * When quick saving the price and stock
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	string	$type			Possible values: 'stockprice', more possibly in the future
	 * @return	void
	 */
	public function adminQuickSave(array &$item_info, $type)
	{
		return;
	}
	
	/**
	 * When removing the item from ibMarket
	 *
	 * @access	public
	 * @return	void
	 */
	public function adminRemove()
	{
		return;
	}
	
	/**
	 * Viewing the file edits if any
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminViewFileEdits()
	{
		return false;
	}
	
	/**
	 * Any admin AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function adminAjax()
	{
		return;
	}
	
	/**
	 * Check and modify the intentory item information before storing in the database
	 *
	 * @access	public
	 * @param	array	&$invItem		The new inventory item information to be stored in the database.
	 * @return	bool	true if the item is successfully created, false otherwise
	 */
	public function publicOnCreateItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When using the item
	 * Show a form and validate the form
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	string|bool				string if there is a form to be used, true if successfully used, false otherwise
	 */
	public function publicUseItem(array &$invItem)
	{
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'public_items' ) );
		
		if($this->request['request_method'] == 'post')
		{
			$member = IPSMember::load($this->request['member_name'],'all','displayname');
			if(!$member)
			{
				$this->registry->getClass('output')->showError('no_member');
			}
			
			if(in_array($member['member_group_id'],$this->itemSettings['protected']))
			{
				$this->registry->getClass('output')->showError('protected_member');
			}
			
			$this->request['amount'] = numbers::parseInt($this->request['amount']);
			if(numbers::compare($this->request['amount']) <= 0)
			{
				$this->registry->getClass('output')->showError('steal_specify_amount');
			}
			if(	numbers::compare($this->request['amount'],$this->itemSettings['amount_min']) < 0 ||
				numbers::compare($this->request['amount'],$this->itemSettings['amount_max']) > 0)
			{
				$this->lang->words['cant_steal_that_much'] = str_replace(array("<#MIN#>","<#MAX#>"),array($this->itemSettings['amount_min'],$this->itemSettings['amount_max']), $this->lang->words['cant_steal_that_much']);
				$this->registry->getClass('output')->showError('cant_steal_that_much');
			}
			
			if(numbers::compare($member[$this->settings['market_members_field']],$this->request['amount']) < 0)
			{
				$this->registry->getClass('output')->showError(array('member_dont_have_that_much',$this->settings['market_currency']));
			}
			
			$rand = numbers::rand(0,100,1);
			$delta_p = numbers::subtract($this->itemSettings['percent_max'],$this->itemSettings['percent_min']);
			$delta_a = numbers::subtract($this->itemSettings['amount_max'],$this->itemSettings['amount_min']);
			$chance = numbers::add(
						numbers::divide(
							numbers::multiply(
								numbers::subtract($this->request['amount'],$this->itemSettings['amount_min']),
								$delta_p),
							$delta_a),
						$this->itemSettings['percent_min']);
			
			$this->deleteItems($invItem);
			
			if(numbers::compare($rand, $chance) < 0)
			{
				IPSMember::save( $this->memberData['member_id'], array( 'core' => array( $this->settings['market_members_field'] => numbers::add($this->memberData[$this->settings['market_members_field']],$this->request['amount']) ) ) );
				IPSMember::save( $member['member_id'], array( 'core' => array( $this->settings['market_members_field'] => numbers::subtract($member[$this->settings['market_members_field']],$this->request['amount']) ) ) );
				
				$this->writeLog($invItem, "Successfully stole ".$this->request['amount']." from <a href='{$this->settings['base_url']}showuser={$member['member_id']}'>{$member['members_display_name']}</a>");
				
				$this->registry->getClass('output')->redirectScreen( $this->lang->words['steal_successful'], $this->settings['base_url_with_app'] . "module=inventory" );
			}
			else
			{
				IPSMember::save( $this->memberData['member_id'], array( 'core' => array( $this->settings['market_members_field'] => numbers::subtract($this->memberData[$this->settings['market_members_field']],numbers::multiply($this->request['amount'],2)) ) ) );
				
				$this->writeLog($invItem, "Failed stealing ".$this->request['amount']." from <a href='{$this->settings['base_url']}showuser={$member['member_id']}'>{$member['members_display_name']}</a>");
				
				$this->registry->getClass('output')->redirectScreen( $this->lang->words['steal_failed'], $this->settings['base_url_with_app'] . "module=inventory" );
			}
		}
		
		return <<<EOF
<form id='stealMoneyForm' method="post" action="{$this->settings['base_url_with_app']}module=inventory&amp;section=item&amp;item={$invItem['i_id']}">
	<div id='stealMoney'class='block_wrap'>
		<h3 id='steaMoneyHeader' class='maintitle'>{$this->item['item_name']}</h3>
		<fieldset class="row2">
			<ul>
				<li>
					<label>{$this->lang->words['howmuchtosteal']}</label>
					<input type='text' name='amount' value='' size='10' id='amount' class='input_text' />
				</li>
				<li>
					<label>{$this->lang->words['which_member']}</label>
					<input type='text' name='member_name' value='' size='32' id='member_name' autocomplete='off' class='input_text' />
				</li>
			</ul>
		</fieldset>
		<fieldset class="submit">
			<input class="input_submit" type="submit" value="{$this->lang->words['steal_submit']}" name="submit" />
		</fieldset>
	</div>
</form>
<script type='text/javascript'>
if( $('member_name') )
{
	new ipb.Autocomplete( $('member_name'), { multibox: false, url: ipb.vars['base_url'] + 'app=core&module=ajax&section=findnames&do=get-member-names&secure_key={$this->member->form_hash}&name=', templates: { wrap: ipb.templates['autocomplete_wrap'], item: ipb.templates['autocomplete_item'] } } );
}
</script>
EOF;
	}
	
	/**
	 * Any AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function publicAjax(array &$invItem)
	{
		return;
	}
	
	/**
	 * When the item is being sold back
	 *
	 * @access	public
	 * @param	array	&$invItem	The inventory item information
	 * @return	bool				true if successful, false otherwise
	 */
	public function publicOnResoldItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When the item is being restocked
	 *
	 * @access	public
	 * @param	array	&$item	The inventory item information
	 * @return	void
	 */
	public function onItemRestock(array &$item)
	{
		return;
	}
	
	/**
	 * Get the description of the item, normally called when there is no intentory item being used
	 *
	 * @access	public
	 * @return	string
	 */
	public function parseStoreDescription()
	{
		return $this->itemDescription;
	}
	
	/**
	 * Get the description of the item, normally called when there is one intentory items being used
	 *
	 * @access	public
	 * @param	array	$invItem	The inventory item information
	 * @return	string
	 */
	public function parseInventoryDescription(array $invItem)
	{
		return $this->itemDescription;
	}
}
